//////////////////////////////////////////////////////////////////////////////////
//	Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include "rcmdapp.h"
#include "mainwnd.h"
#include "helppanel.h"
#include "utils.h"
#include "sysdepapi.h"

#define HELP_PANEL_ID 7886

BEGIN_EVENT_TABLE(CHelpPanel, wxHtmlWindow)
	EVT_HTML_LINK_CLICKED(HELP_PANEL_ID, CHelpPanel::OnCLick)
END_EVENT_TABLE()

CHelpPanel::CHelpPanel(wxWindow *parent):
	wxHtmlWindow(parent, HELP_PANEL_ID, wxDefaultPosition, wxSize(200,400) )
{
	SetMinSize(wxSize(200,400));
}

CHelpPanel::~CHelpPanel(void)
{
}

void CHelpPanel::ShowHelp( cTabPageID page )
{
	wxString strTemplate(wxT("<html><body>%s</body></html>"));
	wxString str;

	switch( page )
	{
		case cTabIDPerfomance:
			str+=wxT("<b>");
			str+= _("Network address translation (NAT) table controller");
			str+=wxT("</b><hr><ul>");
			str+=FormatItem(_("Shows NAT table usage"));
			str+=FormatItem(_("Prevent NAT table overflow problem"));
			str+=FormatItem(_("Prevent router hang problem"));
			str+=FormatItem(_("Speed up peer to peer networks perfomance"));
			str+=FormatItem(_("To configure, go to <a href='o_nat'>Tools/Options</a>"));
			str+=wxT("</ul>");
		
			str+=wxT("<br><br><b>");
			str+= _("Routing table controller");
			str+=wxT("</b><hr><ul>");
			str+=FormatItem(_("Shows Routing table usage"));
			str+=FormatItem(_("Prevent Routing table overflow problem"));
			str+=FormatItem(_("To configure, go to <a href='o_rt'>Tools/Options</a>"));
			str+=wxT("</ul>");
		break;

		case cTabIDConnections:
			str+=wxT("<b>");
			str+= _("Connection list");
			str+=wxT("</b><hr><ul>");
			str+=FormatItem(_("You can drag selected items to other programs"));
			str+=FormatItem(_("Use filter to cut unrelevant items"));
			str+=FormatItem(_("You can use * and ? symbols, in search and filter"));
			str+=wxT("</ul>");
		break;

		case cTabIDSysLog:
		{
			//wxString strRouterIP = wxGetApp().GetMainWnd()->GetLogic().GetRouterIP();
			wxString strIP = wxGetApp().GetMainWnd()->GetLogic().GetCurIP();
			wxString strMess;

			str+=wxT("<b>");
			str+= _("Syslog");
			str+=wxT("</b><hr><ul>");
			str+=FormatItem(_("To enable syslog, go to router's <a href='router'>Web Interface</a>"));

			strMess = _("Set syslog server ip to: ");
			strMess+= strIP.IsEmpty()?_("this PC ip address"):(const wxChar*)strIP;

			str+=FormatItem(strMess);
			str+=FormatItem(_("You can drag selected items to other programs"));
			str+=FormatItem(_("Use filter to cut unrelevant items"));
			str+=FormatItem(_("You can use * and ? symbols, in search and filter"));
			str+=wxT("</ul>");
		}
		break;

		case cTabIDBandwidth:
		{
			str+=wxT("<b>");
			str+= _("Bandwidth");
			str+=wxT("</b><hr><ul>");
			str+=FormatItem(_("Clik to arrow to change graph colour"));
			str+=wxT("</ul>");
		}
		break;

		case cTabIDUsers:
		{
			str+=wxT("<b>");
			str+= _("List of computers connected to the router");
			str+=wxT("</b><hr><ul>");
			str+=FormatItem(_("To allow PC access to router add PC's MAC to white list"));
			str+=FormatItem(_("To block PC access to router add PC's MAC to black list"));
			str+=FormatItem(_("Best practice is add MAC addresses of all friendly computers to the white list and check Block All others option"));
			str+=FormatItem(_("Use drag and drop to drop MAC address from PCs list to white/black list"));
			str+=FormatItem(_("If you by mistake blocked you own PC MAC, remove it from black list and manually restart router"));
			str+=wxT("</ul>");
		}
		break;

		case cTabIDReport:
		{
			str+=wxT("<b>");
			str+= _("IP Settings");
			str+=wxT("</b><hr><ul>");
			str+=FormatItem(_("Can help you to find router external(WAN) IP address"));
			str+=FormatItem(_("You can send this settings to ISP, to help resolve ip settings related problems"));
			str+=wxT("</ul>");
		}
		break;

		case cTabIDPortForward:
		{
			str+=wxT("<b>");
			str+= _("Port forwarding");
			str+=wxT("</b><hr>");
			str+=_("Allows remote computers to connect to a specific computer within a private LAN");
		}
		break;

		case cTabIDADSL:
		{
			str+=wxT("<b>");
			str+= _("ADSL Line Troubleshooting");
			str+=wxT("</b><hr>");
			str+=_("You have bad quality line and need ISP attention if:");
			str+=wxT("<ul>");
			str+=FormatItem(_("CRC Errors per second graph above yellow zone"));
			str+=FormatItem(_("Signal to Noise (SNR) graph below yellow zone"));
			str+=FormatItem(_("Line Attenuation or SNR has bad quality"));
			str+=FormatItem(_("Line Disconnect count grows up"));
			str+=wxT("</ul>");
		}
		break;

		case cTabIDWireless:
		{
			str+=wxT("<b>");
			str+= _("Wireless");
			str+=wxT("</b><hr>");
			str+=_("List of devices connected to the router using wireless access");
		}
		break;
	}

	if ( false==str.IsEmpty() )
	{
		SetPage( wxString::Format(strTemplate,str.c_str()) );
	}
}

void CHelpPanel::ShowErr( int nErrID )
{
	wxString strTemplate(wxT("<html><body>%s</body></html>"));
	wxString str;

	switch( nErrID )
	{
		//telnet ok, but system is not compatible
		case cERFailed:
			str+=wxT("<b>");
			str+= _("Your router is not compatible whith this program");
			str+=wxT("</b><hr>");
			str+=wxT("<ul>");
			str+=FormatItem(_("Please, send router model to <a href='ml'>developer</a>"));
			str+=wxT("</ul>");
		break;

		case cERWrongUserOrPassword:
			str+=wxT("<b>");
			str+= CRTException::FormatReason(cERWrongUserOrPassword);
			str+=wxT("</b><hr>");
			str+=wxT("<ul>");
			str+=FormatItem(_("Go to router's <a href='router'>Web Interface</a>"));
			str+=FormatItem(_("Check password"));
			str+=FormatItem(_("Make sure, that you can <a href='telnet'>establish a telnet session to the router</a>. Type <b>root</b> as user name, then type your password"));
			str+=FormatItem(_("Change admin user name to root, if router denied telnet access as root"));
			str+=FormatItem(_("Run <a href='cw'>connection wizard</a>"));
			str+=FormatItem(_("Change password"));
			str+=wxT("</ul>");
		break;

		default:
		//case cERInvalidHostOrPort:
			str+=wxT("<b>");
			str+=CRTException::FormatReason((cExcReason)nErrID);
			str+=wxT("</b><hr>");
			str+=wxT("<ul>");
			str+=FormatItem(_("Go to router's <a href='router'>Web Interface</a>"));
			str+=FormatItem(_("Make sure that telnet access present and enabled"));
			str+=FormatItem(_("Make sure, that you can <a href='telnet'>establish a telnet session to the router</a>. Type <b>root</b> as user name, then type your password"));
			str+=FormatItem(_("Run <a href='cw'>connection wizard</a>"));
			str+=FormatItem(_("Make sure that telnet port and router ip is correct"));
			str+=wxT("</ul>");
		break;
	}

	if ( !str.IsEmpty() )
	{
		SetPage( wxString::Format(strTemplate, (const wxChar*)str) );
	}
}

wxString CHelpPanel::FormatItem(const wxString& item, int nVal)
{
	wxString str;
	str = wxString::Format(item, nVal);

	return FormatItem(str);
}

wxString CHelpPanel::FormatItem(const wxString& item)
{
	return wxString( wxT("<li>") )+item+wxT("</li>");
}


///////////////////////////////////////////////////////////////////////////
//Events
void CHelpPanel::OnCLick(wxHtmlLinkEvent& event)
{
	wxString strURL =  event.GetLinkInfo().GetHref();

	//mail 
	if ( strURL==wxT("ml") )
	{
		strURL = wxT("mailto:support@routercommander.com?subject=unsupported router");

		std::string  strLog = wxGetApp().GetMainWnd()->GetLogic().GetSessionLog();

		if ( !strLog.empty() )
		{
			strURL+=wxT("&body=");
			strURL+=wxString::FromAscii( MailEncode(strLog).c_str() );
		}

		wxLaunchDefaultBrowser(strURL);
		return;
	}

	//Connection wizard
	if ( strURL==wxT("cw") )
	{
		wxGetApp().GetMainWnd()->ShowConnectionWizard();
		return;
	}

	//Router
	if ( strURL==wxT("router") )
	{
		strURL=wxT("http://");
		strURL+=wxGetApp().GetMainWnd()->GetLogic().GetRouterIP();
		wxLaunchDefaultBrowser(strURL);
		return;
	}

	//Telnet
	if ( strURL==wxT("telnet") )
	{
		strURL=wxT("telnet://");
		strURL+=wxGetApp().GetMainWnd()->GetLogic().GetRouterIP();
		wxLaunchDefaultBrowser(strURL);
		return;
	}

	//Options->Nat
	if ( strURL==wxT("o_nat") )
	{
		wxGetApp().GetMainWnd()->ShowConfig(cPTNAT);
		return;
	}

	//Options->Routing
	if ( strURL==wxT("o_rt") )
	{
		wxGetApp().GetMainWnd()->ShowConfig(cPTRoute);
		return;
	}
	
}

